package top;

/**
 * @author chenyw
 * @date 2022/7/10 22:00
 * @description     将有序数组变成平衡二叉树
 *      递归大法，折半查找找根节点
 */
public class Top108sortedArrayToBST {
    public TreeNode sortedArrayToBST(int[] nums) {
        int n = nums.length;
        return dfs(0, n - 1, nums);

    }
    private TreeNode dfs(int left, int right,int[] nums) {
        if (left > right) {
            return null;
        }
        int mid = (left + right) / 2;
        TreeNode node = new TreeNode(nums[mid]);

        node.left = dfs(left, mid - 1,nums);
        node.right = dfs(mid + 1, right,nums);
        return node;
    }

}
